package com.android.settings.audio.peq.peq.ui;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.core.view.ViewCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsControllerCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.settings.audio.peq.peq.ui.dialog.CommanDialog;
import com.android.settings.R;
import com.android.settings.audio.peq.peq.ui.adapter.DialogAdapter;
import com.android.settings.audio.peq.peq.ui.common.adapter.CommonAdapter;
import com.android.settings.audio.peq.peq.ui.common.adapter.base.ViewHolder;
import com.android.settings.audio.peq.peq.view.LineView;
import com.android.settings.audio.peq.peq.widget.BezierCurveChart;
import com.android.settings.audio.peq.skin.SkinManager;
import com.android.settings.audio.peq.soundeffect.PluginDataManager;
import com.android.settings.audio.peq.soundeffect.SoundEffectConfig;
import com.android.settings.audio.peq.soundeffect.SoundEffectManager;
import com.android.settings.audio.peq.util.LogPlus;
import com.android.settings.audio.peq.util.NetWorkUtils;
import com.android.settings.audio.peq.util.ToastTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SoundEffectPrefabsActivity extends BaseActivity {
    private RecyclerView rlCustom, rlSystem;
    private ISoundEffectPrefabsActivityPresenter prefabsActivityPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getActionBar();
        if (actionBar!=null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            actionBar.setHomeButtonEnabled(false);
            actionBar.hide();
        }
        setContentView(R.layout.peq_activity_sound_effect_prefabs);
        setStatusBarStatus();
        init();
        //setStatusBarHeight(findViewById(R.id.vg_top_nav));
    }

    private void setStatusBarStatus() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.peq_status_bar_background));
        WindowInsetsControllerCompat wic = ViewCompat.getWindowInsetsController(getWindow().getDecorView());
        if (wic != null) {
            // true表示Light Mode，状态栏字体呈黑色，反之呈白色
            boolean isNight = (getApplicationContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_YES) != 0;
            wic.setAppearanceLightStatusBars(!isNight);
        }
    }

    private SoundEffectConfig lastSelectedSoundEffectConfig;

    private void init() {
        ImageView ibBack = findViewById(R.id.imgb_nav_back);
        boolean isNight = (getApplicationContext().getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_YES) != 0;
        ibBack.setColorFilter(isNight ? Color.WHITE : R.color.peq_black_01);
        ibBack.setOnClickListener(v -> finish());
        handleIntent();
        if (dataType == DATA_TYPE_UNKNOWN) {
            ToastTool.showToast(getApplicationContext(), "unknown data type");
            finish();
            return;
        }

        rlCustom = findViewById(R.id.rl_custom);
        rlSystem = findViewById(R.id.rl_system);

        LinearLayoutManager rlCustomLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rlCustom.setLayoutManager(rlCustomLayoutManager);

        rlCustom.setAdapter(new CommonAdapter<SoundEffectConfig>(this, R.layout.peq_layout_sound_effect_item, new ArrayList<>()) {
            @Override
            protected void convert(ViewHolder holder, SoundEffectConfig o, int position) {
                holder.setText(R.id.tv_name, o.getName());
                holder.setVisibility(R.id.iv_check, o.isChecked() ? View.VISIBLE : View.INVISIBLE);
                holder.setVisibility(R.id.iv_action, position == 0 ? View.VISIBLE : View.VISIBLE);
                holder.setVisibility(R.id.iv_cloud, o.getServerDataId() != null ? View.VISIBLE : View.GONE);
                ImageView ivCloud = holder.getView(R.id.iv_cloud);
                if (SkinManager.get().getThemeMode() == SkinManager.BLACK_MODE) {
                    ivCloud.getDrawable().setTint(getResources().getColor(R.color.peq_white_00));
                } else {
                    ivCloud.getDrawable().setTint(getResources().getColor(R.color.peq_black_01));
                }
                holder.setOnClickListener(R.id.iv_action, v -> showActionDialog(prefabsActivityPresenter.getActions(o), o));
                ImageView ivAction = holder.getView(R.id.iv_action);
                if (SkinManager.get().getThemeMode() == SkinManager.BLACK_MODE) {
                    ivAction.setColorFilter(getResources().getColor(R.color.peq_white_00));
                } else {
                    ivAction.setColorFilter(getResources().getColor(R.color.peq_black_01));
                }

                holder.itemView.setOnClickListener(v -> {
                    setResult(o);
                    //finish();
                    if (lastSelectedSoundEffectConfig != null) {
                        lastSelectedSoundEffectConfig.setChecked(false);
                    }
                    lastSelectedSoundEffectConfig = o;
                    lastSelectedSoundEffectConfig.setChecked(true);
                    notifyDataSetChanged();
                    rlSystem.getAdapter().notifyDataSetChanged();

                    //todo 音效立即生效
                    try {
                        doApplyValue(o);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

                holder.itemView.setOnLongClickListener(v -> {
                    if (PluginDataManager.NONE_ID.equals(o.getId())) {
                        return false;
                    }
                    //((SoundEffectPrefabsPresenter) prefabsActivityPresenter).showInfoDialog(o);
                    //((SoundEffectPrefabsPresenter)prefabsActivityPresenter).showPluginEditFormDialog(o);
                    return true;
                });

                FrameLayout flLine = holder.getView(R.id.fl_line);
                flLine.removeAllViews();
                addAndUpdateLineView(flLine, o);
            }
        });

        LinearLayoutManager rlSystemLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rlSystem.setLayoutManager(rlSystemLayoutManager);

        rlSystem.setAdapter(new CommonAdapter<SoundEffectConfig>(this, R.layout.peq_layout_sound_effect_item, new ArrayList<>()) {
            @Override
            protected void convert(ViewHolder holder, SoundEffectConfig o, int position) {
                holder.setText(R.id.tv_name, o.getName());
                holder.setVisibility(R.id.iv_check, o.isChecked() ? View.VISIBLE : View.INVISIBLE);
                holder.setVisibility(R.id.iv_action, View.INVISIBLE);
                holder.setVisibility(R.id.iv_cloud, View.GONE);
                ImageView ivAction = holder.getView(R.id.iv_action);
                if (SkinManager.get().getThemeMode() == SkinManager.BLACK_MODE) {
                    ivAction.setColorFilter(getResources().getColor(R.color.peq_white_00));
                } else {
                    ivAction.setColorFilter(getResources().getColor(R.color.peq_black_01));
                }
                holder.itemView.setOnClickListener(v -> {
                    setResult(o);
                    //finish();
                    if (lastSelectedSoundEffectConfig != null) {
                        lastSelectedSoundEffectConfig.setChecked(false);
                    }
                    lastSelectedSoundEffectConfig = o;
                    lastSelectedSoundEffectConfig.setChecked(true);
                    notifyDataSetChanged();
                    rlCustom.getAdapter().notifyDataSetChanged();

                    //todo 音效立即生效
                    try {
                        doApplyValue(o);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });

                FrameLayout flLine = holder.getView(R.id.fl_line);
                flLine.removeAllViews();
                addAndUpdateLineView(flLine, o);
            }
        });

        findViewById(R.id.ll_custom).setOnClickListener(v -> {
            rlCustom.setVisibility(rlCustom.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
            TextView tvExpandIc = findViewById(R.id.tvExpandIcCustom);
            tvExpandIc.setText(rlCustom.getVisibility() == View.VISIBLE ? "▽" : "△");
        });
        findViewById(R.id.ll_sys).setOnClickListener(v -> {
            rlSystem.setVisibility(rlSystem.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
            TextView tvExpandIc = findViewById(R.id.tvExpandIcSys);
            tvExpandIc.setText(rlSystem.getVisibility() == View.VISIBLE ? "▽" : "△");
        });

        findViewById(R.id.ll_sys).setVisibility(View.GONE);
        rlSystem.setVisibility(View.GONE);

        loadData();

        prefabsActivityPresenter = new SoundEffectPrefabsPresenter(this);
    }

    private void addAndUpdateLineView(FrameLayout flLine, SoundEffectConfig o) {
        if (dataType == DATA_TYPE_EQ) {
            BezierCurveChart bezierCurveChart = new BezierCurveChart(SoundEffectPrefabsActivity.this);
            //todo 数据
            final List<BezierCurveChart.Point> eqPoints = new ArrayList<>();
            float[] fs = null;
            if (o.isSystem()) {//系统预设数据
                final Map<String, float[]> eqPointsForSystem = new HashMap<>();
                //eqPointsForSystem.put(NameString.getResoucesString(this, R.string.equalizer_blues), EqActivity.blues);
                //eqPointsForSystem.put(NameString.getResoucesString(this, R.string.equalizer_classic), EqActivity.classic);
                //eqPointsForSystem.put(NameString.getResoucesString(this, R.string.equalizer_dance), EqActivity.dance);
                //eqPointsForSystem.put(NameString.getResoucesString(this, R.string.equalizer_jazz), EqActivity.jazz);
                //eqPointsForSystem.put(NameString.getResoucesString(this, R.string.equalizer_metal), EqActivity.metal);
                //eqPointsForSystem.put(NameString.getResoucesString(this, R.string.equalizer_pop), EqActivity.pop);
                //eqPointsForSystem.put(NameString.getResoucesString(this, R.string.equalizer_rock), EqActivity.rock);
                //eqPointsForSystem.put(NameString.getResoucesString(this, R.string.equalizer_voice), EqActivity.voice);
                fs = eqPointsForSystem.get(o.getName());
            } else {
                try {
                    fs = getEqFs(o);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (fs != null) {
                for (int i = 0, n = fs.length; i < n; i++) {
                    if (i > 0) {
                        eqPoints.add(new BezierCurveChart.Point(i - 1, (fs[i] * 2 + 24) - 0.1f));
                    }
                }
            }
            if (!eqPoints.isEmpty()) {
                bezierCurveChart.init(eqPoints, null, "");
                flLine.addView(bezierCurveChart);
            }
        } else if (dataType == DATA_TYPE_MSEB) {
            if (true) return;
            LineView lineView = new LineView(SoundEffectPrefabsActivity.this);
            List<List<double[]>> values = null;
            try {
                values = getMsebValues(o);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (values != null) {
                flLine.addView(lineView.execute2("", ""), new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

                List<double[]> xValuesList = values.get(0);
                List<double[]> yValuesList = values.get(1);
                double preAmp = 0;
                lineView.updateLines(new String[yValuesList.size()], xValuesList, yValuesList, true, preAmp);

                int backgroundColor = 0xffffffff;
                int lineColor = Color.BLACK;
                if (SkinManager.get().getThemeMode() == SkinManager.BLACK_MODE) {
                    backgroundColor = 0x00000000;
                    lineColor = Color.WHITE;
                }
                lineView.setShow(false, true, false, false, backgroundColor, lineColor);
            }
        } else if (dataType == DATA_TYPE_PEQ) {
            LineView lineView = new LineView(SoundEffectPrefabsActivity.this);
            if (o.isSystem() && NetWorkUtils.isConnected(getApplicationContext())) {
                //系统在线数据需要下载
                new Thread(() -> {
                    List<List<double[]>> values = null;
                    try {
                        values = getPeqValues(o);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (values != null) {
                        final List<List<double[]>> final_values = values;
                        runOnUiThread(() -> {
                            flLine.addView(lineView.execute2("", ""), new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

                            List<double[]> xValuesList = final_values.get(0);
                            List<double[]> yValuesList = final_values.get(1);
                            double preAmp = 0;
                            lineView.updateLines(new String[yValuesList.size()], xValuesList, yValuesList, true, preAmp);

                            int backgroundColor = 0xffffffff;
                            int lineColor = Color.BLACK;
                            if (SkinManager.get().getThemeMode() == SkinManager.BLACK_MODE) {
                                backgroundColor = 0x00000000;
                                lineColor = Color.WHITE;
                            }
                            lineView.setShow(false, true, false, false, backgroundColor, lineColor);
                        });
                    }
                }).start();
                return;
            }
            List<List<double[]>> values = null;
            try {
                values = getPeqValues(o);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (values != null) {
                flLine.addView(lineView.execute2("", ""), new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

                List<double[]> xValuesList = values.get(0);
                List<double[]> yValuesList = values.get(1);
                double preAmp = 0;
                lineView.updateLines(new String[yValuesList.size()], xValuesList, yValuesList, true, preAmp);

                int backgroundColor = 0xffffffff;
                int lineColor = Color.BLACK;
                if (SkinManager.get().getThemeMode() == SkinManager.BLACK_MODE) {
                    backgroundColor = 0x00000000;
                    lineColor = Color.WHITE;
                }
                lineView.setShow(false, true, false, false, backgroundColor, lineColor);
            }
        }
    }

    private List<List<double[]>> getPeqValues(SoundEffectConfig o) throws Exception {
        return prefabsActivityPresenter.getPeqValues(o);
    }

    private List<List<double[]>> getMsebValues(SoundEffectConfig o) throws Exception {
        return prefabsActivityPresenter.getMsebValues(o);
    }


    private float[] getEqFs(SoundEffectConfig o) throws Exception {
        return prefabsActivityPresenter.getEqFs(o);
    }

    private void loadData() {
        new Thread(() -> {
            //合并已提交到服务器端的数据
            List<SoundEffectConfig> localSoundEffectConfigs = loadPrefabsImpl.loadCustoms();
            if (localSoundEffectConfigs != null) {
                //当前本地已保存到服务器的配置项
                Map<String, SoundEffectConfig> localSavedToServerConfigsMap = new HashMap<>();
                for (SoundEffectConfig soundEffectConfig : localSoundEffectConfigs) {
                    if (soundEffectConfig.getServerDataId() != null) {
                        localSavedToServerConfigsMap.put(soundEffectConfig.getServerDataId(), soundEffectConfig);
                    }

                    soundEffectConfig.setChecked(soundEffectConfig.equals(selectedSoundEffectConfig));
                    if (soundEffectConfig.isChecked()) {
                        lastSelectedSoundEffectConfig = soundEffectConfig;
                    }
                }

                //已缓存的服务器端配置数据 与本地已有的数据进行合并, 如果有本地未记录的数据则添加
                List<SoundEffectConfig> cachedConfigs = loadPrefabsImpl.loadCachedConfigsOnServer();
                if (cachedConfigs != null) {
                    for (SoundEffectConfig soundEffectConfig : cachedConfigs) {
                        //如果有本地未记录的数据则添加
                        if (!localSavedToServerConfigsMap.containsKey(soundEffectConfig.getId()) && !localSoundEffectConfigs.contains(soundEffectConfig)) {
                            soundEffectConfig.setServerDataId(soundEffectConfig.getId());
                            localSoundEffectConfigs.add(soundEffectConfig);
                        }
                    }
                }

                //从服务器拉已保存的数据 与本地已有的数据进行合并, 如果有本地未记录的数据则添加
                boolean isNetworkAvailable = NetWorkUtils.isConnected(getApplicationContext());
                if (isNetworkAvailable) {
                    List<SoundEffectConfig> customConfigsOnServer = loadPrefabsImpl.loadCustomsOnServer();
                    if (customConfigsOnServer != null) {
                        for (SoundEffectConfig soundEffectConfig : customConfigsOnServer) {
                            //如果有本地未记录的数据则添加
                            if (!localSavedToServerConfigsMap.containsKey(soundEffectConfig.getId()) && !localSoundEffectConfigs.contains(soundEffectConfig)) {
                                soundEffectConfig.setServerDataId(soundEffectConfig.getId());
                                localSoundEffectConfigs.add(soundEffectConfig);
                            }
                        }
                    }
                }

                for (SoundEffectConfig config : localSoundEffectConfigs) {
                    config.setChecked(config.equals(selectedSoundEffectConfig));
                    if (config.isChecked()) {
                        lastSelectedSoundEffectConfig = config;
                    }
                }

                runOnUiThread(() -> {
                    ((CommonAdapter) rlCustom.getAdapter()).clearData();
                    ((CommonAdapter) rlCustom.getAdapter()).addData(localSoundEffectConfigs);

                    //findViewById(R.id.tv_custom_empty).setVisibility(soundEffectConfigsCustom.isEmpty() ? View.VISIBLE : View.GONE);
                    rlCustom.setVisibility(localSoundEffectConfigs.isEmpty() ? View.GONE : View.VISIBLE);
                    findViewById(R.id.ll_custom).setVisibility(localSoundEffectConfigs.isEmpty() ? View.GONE : View.VISIBLE);
                });
            }
            //todo 加载系统预设数据
            List<SoundEffectConfig> cachedSystemPrefabs = loadPrefabsImpl.loadCachedSystems();

            List<SoundEffectConfig> soundEffectConfigsSystem = null;
            try {
                soundEffectConfigsSystem = loadPrefabsImpl.loadSystems();
            } catch (Exception e) {
                e.printStackTrace();
                soundEffectConfigsSystem = cachedSystemPrefabs;
            }
            if (soundEffectConfigsSystem != null) {
                //todo 下载系统预设数据并缓存
                boolean isNetworkAvailable = NetWorkUtils.isConnected(getApplicationContext());
                if (isNetworkAvailable) {
                    for (SoundEffectConfig soundEffectConfig : soundEffectConfigsSystem) {
                        if (TextUtils.isEmpty(soundEffectConfig.getDataUrl())) {
                            continue;
                        }
                        try {
                            String json = prefabsActivityPresenter.download(soundEffectConfig.getDataUrl());
                            soundEffectConfig.setSystemData(json);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    SoundEffectManager.getInstance().cacheSystemPrefabs(SoundEffectPrefabsPresenter.convertDataType(dataType), soundEffectConfigsSystem);
                }


                if (selectedSoundEffectConfig != null) {
                    for (SoundEffectConfig soundEffectConfig : soundEffectConfigsSystem) {
                        soundEffectConfig.setChecked(soundEffectConfig.equals(selectedSoundEffectConfig));
                        if (soundEffectConfig.isChecked()) {
                            lastSelectedSoundEffectConfig = soundEffectConfig;
                        }
                    }
                }
                List<SoundEffectConfig> finalSoundEffectConfigsSystem = soundEffectConfigsSystem;
                runOnUiThread(() -> {
                    ((CommonAdapter) rlSystem.getAdapter()).clearData();
                    ((CommonAdapter) rlSystem.getAdapter()).addData(finalSoundEffectConfigsSystem);

                    //findViewById(R.id.tv_system_empty).setVisibility(soundEffectConfigsSystem.isEmpty() ? View.VISIBLE : View.GONE);
                    rlSystem.setVisibility(finalSoundEffectConfigsSystem.isEmpty() ? View.GONE : View.VISIBLE);
                    findViewById(R.id.tv_sys_title).setVisibility(finalSoundEffectConfigsSystem.isEmpty() ? View.GONE : View.VISIBLE);
                    findViewById(R.id.ll_sys).setVisibility(finalSoundEffectConfigsSystem.isEmpty() ? View.GONE : View.VISIBLE);
                });
            }
        }).start();
    }

    public void refreshData() {
        loadData();
    }

    private void showActionDialog(final List<ActionPair> itemsList, SoundEffectConfig soundEffectConfig) {
        final CommanDialog dialog = new CommanDialog(this, R.style.peqPopDialogStyle, CommanDialog.STYLE_POP_NOBUTTON);
        dialog.setCanceledOnTouchOutside(true);
        dialog.addBottomView(R.layout.peq_dialog_listview_3);
        dialog.setCanceledOnTouchOutside(true);

        ListView listView = dialog.getContentView().findViewById(R.id.dialog_listview);
        TextView titleTextView = dialog.titleTextView;
        titleTextView.setText(soundEffectConfig.getName());
        final List<String> items = new ArrayList<>();
        final Map<String, OnActionListener> onActionListenerMap = new HashMap<>();
        for (ActionPair actionPair : itemsList) {
            items.add(actionPair.name);
            onActionListenerMap.put(actionPair.name, actionPair.onActionListener);
        }
        DialogAdapter dialogAdapter = new DialogAdapter(this, items, false, true);
        listView.setAdapter(dialogAdapter);
        listView.setOnItemClickListener((arg0, arg1, pos, arg3) -> {
            String clickItemString = items.get(pos);
            OnActionListener onActionListener = onActionListenerMap.get(clickItemString);
            if (onActionListener != null) onActionListener.onClick(clickItemString, soundEffectConfig);
            dialog.dismiss();
        });
        dialog.show();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    private static final String DATA_TYPE = "dataType";
    public static final int RESULT_CODE = 22323;
    public static final String RESULT_NAME = "selectedSoundEffectConfig";
    public static final String SELECTED_SOUND_EFFECT = "SELECTED_SOUND_EFFECT";

    public static void startForResult(Activity activity, int type, IDataTypeHandler loadPrefabsImpl, SoundEffectConfig selectedSoundEffectConfig, int requestCode) {
        SoundEffectPrefabsActivity.loadPrefabsImpl = loadPrefabsImpl;
        Intent it = new Intent(activity, SoundEffectPrefabsActivity.class);
        it.putExtra(DATA_TYPE, type);
        it.putExtra(SELECTED_SOUND_EFFECT, selectedSoundEffectConfig);
        //activity.startActivity(it);
        activity.startActivityForResult(it, requestCode);
    }

    private static IDataTypeHandler loadPrefabsImpl;

    public interface IDataTypeHandler {
        List<SoundEffectConfig> loadCustoms();

        List<SoundEffectConfig> loadSystems() throws Exception;

        List<SoundEffectConfig> loadCustomsOnServer();

        List<SoundEffectConfig> loadCachedConfigsOnServer();

        List<SoundEffectConfig> loadCachedSystems();
    }

    public interface OnActionListener {
        void onClick(String action, SoundEffectConfig soundEffectConfig);
    }


    public void setSelectedSoundEffectConfig(SoundEffectConfig soundEffectConfig) {
        this.selectedSoundEffectConfig = soundEffectConfig;
    }

    public void setResult(SoundEffectConfig soundEffectConfig) {
        Intent data = new Intent();
        data.putExtra(RESULT_NAME, soundEffectConfig);
        setResult(RESULT_CODE, data);
    }

    private int dataType;
    private SoundEffectConfig selectedSoundEffectConfig;

    private void handleIntent() {
        Intent it = getIntent();
        if (it == null) return;
        dataType = it.getIntExtra(DATA_TYPE, DATA_TYPE_UNKNOWN);
        selectedSoundEffectConfig = (SoundEffectConfig) it.getSerializableExtra(SELECTED_SOUND_EFFECT);
        if (selectedSoundEffectConfig == null) {
            selectedSoundEffectConfig = new SoundEffectConfig(PluginDataManager.NONE_ID, getString(R.string.peq_mseb_none), false);
        }
    }

    public static final int DATA_TYPE_EQ = 0;
    public static final int DATA_TYPE_MSEB = 1;
    public static final int DATA_TYPE_PEQ = 2;
    public static final int DATA_TYPE_UNKNOWN = -1;

    public int getDataType() {
        return dataType;
    }

    public static class ActionPair {
        public String name;
        public OnActionListener onActionListener;

        public ActionPair(String name, OnActionListener onActionListener) {
            this.name = name;
            this.onActionListener = onActionListener;
        }
    }


    /**
     * 音效 apply
     *
     * @param soundEffectConfig
     */
    private void doApplyValue(SoundEffectConfig soundEffectConfig) throws Exception {
        if (prefabsActivityPresenter instanceof SoundEffectPrefabsPresenter) {
            ((SoundEffectPrefabsPresenter) prefabsActivityPresenter).doApplyValue(soundEffectConfig);
        }
    }
}
